package com.xdhl.fu.fim.xxljob.job;

import com.xdhl.fu.common.core.task.IJob;
import com.xdhl.fu.common.core.utils.bean.BeanUtils;
import com.xdhl.fu.fim.repository.MGFlightAirportInfoRepository;
import com.xdhl.fu.fim.service.IFlightAirportInfoService;
import com.xdhl.fu.flight.info.api.domain.FlightAirportInfo;
import com.xdhl.fu.flight.info.api.mongodb.domain.MGFlightAirportInfo;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 机场信息同步执行器
 *
 * @author 朱力维
 * @time 2022/6/3 11:59
 */
@Component
public class MGAirportInfoSyncJob implements IJob {

    private static final Logger log = LoggerFactory.getLogger(MGAirportInfoSyncJob.class);

    @Resource
    private IFlightAirportInfoService flightAirportInfoService;

    @Resource
    private MGFlightAirportInfoRepository mgFlightAirportInfoRepository;

    @Transactional
    @XxlJob(value = "mgAirportInfoSyncJob", init = "init", destroy = "destroy")
    public ReturnT<String> execute(String param) {
        this.start();
        log.info("机场信息已同步完成。");
        return ReturnT.SUCCESS;
    }

    @Override
    public void start() {
        // 查询新数据
        List<FlightAirportInfo> flightAirportInfos = flightAirportInfoService.list();
        // 删除旧数据
        mgFlightAirportInfoRepository.deleteAll();
        for (FlightAirportInfo flightAirportInfo : flightAirportInfos) {
            MGFlightAirportInfo mgFlightAirportInfo = new MGFlightAirportInfo();
            BeanUtils.copyBeanProp(mgFlightAirportInfo, flightAirportInfo);
            // 缓存新数据
            mgFlightAirportInfoRepository.save(mgFlightAirportInfo);
        }
    }

    @Override
    public void init() {

    }

    @Override
    public void destroy() {

    }

}
